package com.vhall.component.controller.rbac.adm;


import com.vhall.component.entity.rbac.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.utils.StringUtils;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.service.annotation.OperateLog;
import com.vhall.component.service.rbac.RbacActionService;
import com.vhall.component.service.rbac.RbacAdminMenuService;
import com.vhall.component.service.rbac.RbacRoleService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import java.util.List;


/**
 * @author dalong
 * @date 2021-05-12 20:55
 */

@Api(tags = "RbacAdminRoleController", value = "admin角色")
@Slf4j
@VhallComponent
@Tag(name = "角色列表-管理端")
public class RbacAdminRoleController implements RbacAdminRoleFacade {

    @Autowired
    private RbacRoleService roleService;
    @Autowired
    private RbacActionService actionService;

    @Autowired
    private RbacAdminMenuService rbacAdminMenuService;

    /**
     * 获取角色信息
     *
     * @param reqVO 请求参数
     * @return 响应结果
     */
    @Override
    @TokenAuth("sys:role:info")
    public RoleAdminGetRspVO getRoleInfo(@Validated RoleAdminGetReqVO reqVO) {
        return roleService.getRoleForAdmin(reqVO);
    }

    /**
     * 获取角色列表
     *
     * @param reqVO 请求参数
     * @return 响应结果
     */
    @Override
    @TokenAuth("sys:role:list")
    public RoleAdminListRspVO getRoleList(@Validated RoleAdminListReqVO reqVO) {
        if (StringUtils.isNotEmpty(reqVO.getKeyword())) {
            String keyword = reqVO.getKeyword();
            if (reqVO.getKeyword().contains(CommonConstant.UNDERLINE)) {
                keyword = reqVO.getKeyword().replace(CommonConstant.UNDERLINE, "\\" + CommonConstant.UNDERLINE);
            }
            if (reqVO.getKeyword().contains(CommonConstant.PERCENT_SIGN)) {
                keyword = keyword.replace(CommonConstant.PERCENT_SIGN, "\\" + CommonConstant.PERCENT_SIGN);
            }
            reqVO.setKeyword(keyword);
        }
        return roleService.getRoleListForAdmin(reqVO);
    }

    /**
     * 角色添加
     *
     * @param reqVo 请求参数
     * @return 响应结果
     */
    @Override
    @TokenAuth("sys:role:add")
    @OperateLog(type = OperateTypeEnum.CREATE, name = "角色添加")
    public RoleAdminAddRspVO addRole(@Validated RoleAdminAddReqVO reqVo) {
        return roleService.addRoleForAdmin(reqVo);
    }

    /**
     * 角色编辑
     *
     * @param reqVo 请求参数
     * @return 响应结果
     */
    @Override
    @TokenAuth("sys:role:edit")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "角色编辑")
    public Integer editRole(@Validated RoleAdminEditReqVO reqVo) {
        return roleService.editRoleForAdmin(reqVo);
    }

    /**
     * 角色删除
     *
     * @param reqVo 请求参数
     * @return 响应结果
     */
    @Override
    @TokenAuth("sys:role:del")
    @OperateLog(type = OperateTypeEnum.DELETE, name = "角色删除")
    public List<Integer> deleteRole(@Validated RoleAdminDeleteReqVO reqVo) {
        return roleService.deleteRoleForAdmin(reqVo);
    }

    @TokenAuth("sys:menu:edit")
    @Override
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "编辑菜单权限")
    public Boolean editMenues(EditMenuesReqVO vo) {
        return roleService.editMenues(vo);
    }

    @TokenAuth("sys:action:edit")
    @Override
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "编辑操作权限")
    public Boolean editAction(EditActionReqVO reqVO) {
        return actionService.edit(reqVO);
    }

    @TokenAuth("sys:menu:list")
    @Override
    public List<AdminMenuListRspVo> menuList(AdminMenuListReqVo reqVo) {
        reqVo.setStatus((byte) 1);
        return rbacAdminMenuService.getList(reqVo);
    }

    @TokenAuth("sys:action:list")
    @Override
    public List<ActionsRspVO> actionList(ActionListReqVO reqVO) {
        reqVO.setStatus((byte) 1);
        return actionService.getList(reqVO).getData();
    }
}
